﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookClubPortal.Models;
using BookClubPortal.Filters;
using WebMatrix.WebData;

namespace BookClubPortal.Controllers
{
    [HandleError]
    [AuthorizeByDB]
    public class BrowseController : Controller
    {
        BookClubPortalEntities storeDB = new BookClubPortalEntities();

        //
        // GET: /Browse/
        [AuthorizeByDB]
        public ActionResult Index(string TitleString = "", string AuthorString = "", int bookGenre = -200)
        {

            ViewBag.bookGenre = new SelectList(storeDB.Genres.OrderBy(a=>a.Name), "GenreId", "Name");
        var books = storeDB.Books.Include("Genre").Where(b => b.UserId != WebSecurity.CurrentUserId);

        if (!String.IsNullOrEmpty(TitleString))
        {
            books = books.Where(s => s.Title.Contains(TitleString));
        }
        if (!String.IsNullOrEmpty(AuthorString))
        {
            books = books.Where(s => s.Author.Contains(AuthorString));
        }

        if (bookGenre == -200)
            return View(books);
        else
        {
            return View(books.Where(x => x.GenreId == bookGenre));
        }

       
        //return View(books.ToList());

        }

        //
        // GET: /Browse/Details/5

        public ViewResult Details(int id)
        {
            Book book = storeDB.Books.Find(id);
            book.Genre = storeDB.Genres.Find(book.GenreId);
            return View(book);
        }

        //
        // GET: /Browse/Buy/5

        public ViewResult Buy(int id)
        {
            Book book = storeDB.Books.Find(id);
            book.Genre = storeDB.Genres.Find(book.GenreId);
            return View(book);
        }

        //
        // POST: /Browse/Buy/5

        [HttpPost, ActionName("Buy")]
        public ActionResult BuyConfirmed(int id)
        {
            // Get the book
            Book book = storeDB.Books.Find(id);
            Genre genre = storeDB.Genres.Find(book.GenreId);
            String descr="";
            if (book.Description != null) descr =descr = book.Description;   

              // Send email to both sides
            User userCustomer = storeDB.Users.Find(WebSecurity.CurrentUserId);
            User userSeller = storeDB.Users.Find(book.UserId);
            MvcApplication.SendEmail(userCustomer.UserName,
                                            "Book Club Order Confirmation",
                                            "Dear Member,\n\n" +
                                            "You have ordered the following book from us:\n" +
                                            "Title: "+book.Title+
                                            "\nAuthor: " + book.Author +
                                            "\nRelease Year: " + book.ReleaseYear.ToString() +
                                            "\nGenre: " +genre.Name  +
                                            "\nDescription: " + descr +
                                            "\n\nPlease contact the owner on the following e-mail address: " + userSeller.UserName +
                                            "\n\nThank you for using our services!");
            MvcApplication.SendEmail(userSeller.UserName,
                                           "Book Club Order Notification",
                                           "Dear Member,\n\n" +
                                           "The following book was ordered from You and removed from our database:\n" +
                                           "Title: " + book.Title +
                                           "\nAuthor: " + book.Author +
                                           "\nRelease Year: " + book.ReleaseYear.ToString() +
                                           "\nGenre: " + genre.Name +
                                           "\nDescription: " + descr +
                                           "\n\nPlease contact the buyer on the following e-mail address: " + userCustomer.UserName +
                                           "\n\nThank you for using our services!");
           
            
              // Remove book from list
              storeDB.Books.Remove(book);
              storeDB.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}
